package com.itheima.health.utils;

import com.itheima.health.utils.Helper.HttpUtils;
import org.apache.http.HttpResponse;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 2024/7/8-16:25
 * 功能:
 */
public class SMSUtil {
    static final String APPCODE = "6773cbcf3f784a07a8c269017c0402b5";

    public static void main(String[] args) {
//        sent();
        System.out.println(generateVerifyCode());
    }

    private static void sent(String phone) {
        String host = "https://smsv2.market.alicloudapi.com";
        String path = "/sms/sendv2";
        String method = "GET";
        String code = generateVerifyCode();
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + APPCODE);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", "14779834841");
        querys.put("content", "【牛马健康】\n提示：您的验证码是"+code+"。如非本人操作，请忽略本短信");


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static final Random RANDOM = new Random();

    /**
     * 生成6位数字验证码
     *
     * @return 6位数字验证码
     */
    public static String generateVerifyCode() {
        int code = RANDOM.nextInt(999999); // 生成0-999998之间的随机数
        return String.format("%06d", code); // 使用format方法保证始终返回6位数字，不足6位前面补0
    }

}
